﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Web.Services.Protocols;
using Microsoft.Crm.Sdk;
using Microsoft.Crm.Sdk.Metadata;
using Microsoft.Crm.SdkTypeProxy;
using Microsoft.Crm.SdkTypeProxy.Metadata;

namespace CSP.CRM.Deployer
{
  public class CspCrmDeployer
  {
    private CrmService crm;
    private MetadataService meta;
    private TextWriter log;
    private string organizationName;
    private string crmUserLoginName; // User the internet portal will impersonate to connect to CRM
    private Uri crmServerUri;

    private int languageCode = 1033;
    public int LanguageCode
    {
      get { return languageCode; }
      set { languageCode = value; }
    }


    public CspCrmDeployer(Uri crmServerUri, string organizationName, string crmUserLoginName, TextWriter log)
    {
      this.crmServerUri = crmServerUri;
      this.organizationName = organizationName;
      this.crmUserLoginName = crmUserLoginName;
      this.log = log;

      crm = new CrmService();
      crm.CrmAuthenticationTokenValue = new CrmAuthenticationToken();
      crm.CrmAuthenticationTokenValue.OrganizationName = organizationName;
      crm.Url = crmServerUri + @"/mscrmservices/2007/crmservice.asmx";
      crm.UseDefaultCredentials = true;

      meta = new MetadataService();
      meta.CrmAuthenticationTokenValue = new CrmAuthenticationToken();
      meta.CrmAuthenticationTokenValue.OrganizationName = organizationName;
      meta.Url = crmServerUri + @"/mscrmservices/2007/metadataservice.asmx";
      meta.UseDefaultCredentials = true;
    }

    public void Run()
    {
      log.WriteLine("---------------- ISV Extensions");
      ISVExtensionsDeployer isvDep = new ISVExtensionsDeployer("csp_", organizationName, log);
      isvDep.Run();

      log.WriteLine("---------------- CRM Customization");
      CustomizationDeployer custDep = new CustomizationDeployer(crm, meta, log);
      if (languageCode != 1033)
        custDep.LanguageCode = languageCode;
      custDep.Run();
      string[] procedureNames = custDep.GetProcedureNames();

      log.WriteLine("---------------- CSP Plugins");
      PluginDeployer pluginDep = new PluginDeployer(crm, "csp_", procedureNames, log);
      pluginDep.Run();

      log.WriteLine("---------------- CSP custom security role");
      RoleDeployer roleDep = new RoleDeployer(crm, crmUserLoginName);
      roleDep.Run();

    }

    public static void TestConnection(Uri testServerUri, string testUser, string testPassword, string testDomain, string testOrganizationName)
    {
      try
      {
        CrmService testCrm = new CrmService();
        testCrm.CrmAuthenticationTokenValue = new CrmAuthenticationToken();
        testCrm.CrmAuthenticationTokenValue.OrganizationName = testOrganizationName;
        testCrm.Credentials = new NetworkCredential(testUser, testPassword, testDomain);
        testCrm.Url = testServerUri.ToString() + "/mscrmservices/2007/crmservice.asmx";

        WhoAmIRequest req = new WhoAmIRequest();
        WhoAmIResponse resp = (WhoAmIResponse)testCrm.Execute(req);
      }
      catch (SoapException soapEx)
      {
        throw new Exception(soapEx.Detail.InnerText, soapEx);
      }


    }
  }
}
